﻿using DevExpress.XtraEditors;
using DevExpress.XtraEditors.Controls;
using DevExpress.XtraGrid;
using EyFramework.Common;
using EyFramework.Library.Select;
using EyFramework.Sys.Core.Service;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using EyFramework.Sys.Entity;

//using EyFramework.Sys.Core.Service;
//using EyFramework.Sys.Entity;

namespace EyFramework.Library
{
    public static class SysUtil
    {
        #region 设置SearchLookupEdit的行号

        public static void ShowRowNum(this DevExpress.XtraGrid.Views.Grid.GridView gridView)
        {
            gridView.CustomDrawRowIndicator += gridView_CustomDrawRowIndicator;
            gridView.IndicatorWidth = 30;
        }

        public static void ShowContextMenu(this DevExpress.XtraGrid.GridControl gridControl, IContainer container)
        {
            if (container == null)
            {
                container = new System.ComponentModel.Container();
                Console.WriteLine("Container 容器未初始化，绑定右键菜单失败！");
            }

            ContextMenuStrip contextMenu = new ContextMenuStrip(container);
            ToolStripMenuItem 导出ExcelEToolStripMenuItem = new ToolStripMenuItem();
            gridControl.ContextMenuStrip = contextMenu;
            contextMenu.Items.AddRange(new ToolStripItem[] {
                导出ExcelEToolStripMenuItem});
            contextMenu.Name = "gridMenu";
            contextMenu.Size = new System.Drawing.Size(105, 26);
            导出ExcelEToolStripMenuItem.Image = Properties.Resources.Excel;
            导出ExcelEToolStripMenuItem.Name = "exportMenu";
            导出ExcelEToolStripMenuItem.Size = new System.Drawing.Size(152, 22);
            导出ExcelEToolStripMenuItem.Text = "导出Excel(&E)";
            导出ExcelEToolStripMenuItem.Tag = gridControl;
            导出ExcelEToolStripMenuItem.Click += 导出ExcelEToolStripMenuItem_Click;
        }

        public static void 导出ExcelEToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ToolStripMenuItem menu = (ToolStripMenuItem)sender;
            if (menu.Tag != null)
            {
                DevExpress.XtraGrid.GridControl exportGrid = (GridControl)menu.Tag;
                SaveFileDialog fileDialog = new SaveFileDialog();
                fileDialog.InitialDirectory = EyFramework.Common.Utils.DirectoryUtil.GetCurrentDirectory();
                fileDialog.Title = "导出Excel";
                fileDialog.DefaultExt = "xlsx";
                fileDialog.Filter = "Excel 2007文件|*.xlsx|Excel 99-03文件|*.xls";
                DialogResult dialogResult = fileDialog.ShowDialog();
                if (dialogResult == DialogResult.OK)
                {
                    DevExpress.XtraPrinting.XlsExportOptions options = new DevExpress.XtraPrinting.XlsExportOptions();
                    exportGrid.ExportToXlsx(fileDialog.FileName);
                    if (Globals.ShowYesNoAndTips("恭喜您，导出成功，是否立即打开？") == System.Windows.Forms.DialogResult.Yes)
                    {
                        System.Diagnostics.Process.Start(fileDialog.FileName);
                    }
                }
            }
        }

        private static void gridView_CustomDrawRowIndicator(object sender, DevExpress.XtraGrid.Views.Grid.RowIndicatorCustomDrawEventArgs e)
        {
            if (e.RowHandle >= 0 && e.Info.IsRowIndicator)
            {
                e.Info.DisplayText = (e.RowHandle + 1).ToString();
            }
        }

        #endregion 设置SearchLookupEdit的行号

        /// <summary>
        /// 员工输入器
        /// </summary>
        /// <param name="search"></param>
        public static void InitUserSelect(this BaseSearchLookUpEdit search)
        {
            search.Properties.NullText = String.Empty;
            ISysUserService sysUserService = Globals.GetService<ISysUserService>();
            List<SeachColumn> columns = new List<SeachColumn>();
            columns.Add(new SeachColumn("USER_NAME", "员工工号", SEARCHCOLUMNTYPE.ID));
            columns.Add(new SeachColumn("NICK_NAME", "员工名称", SEARCHCOLUMNTYPE.DISPLAY));
            columns.Add(new SeachColumn("SPELL_CODE", "拼音码", SEARCHCOLUMNTYPE.COL));
            columns.Add(new SeachColumn("FIVE_CODE", "五笔码", SEARCHCOLUMNTYPE.COL));
            search.InitData(sysUserService.SelectAllUsers().ConvertAll(s => (object)s), columns);
        }

        /// <summary>
        /// 初始化系统列表的下来选择器
        /// </summary>
        /// <param name="comboBox"></param>
        public static void InitLueSysList(this LookUpEdit lookUp)
        {
            lookUp.Properties.NullText = "";
            ISysListService sysListService = Globals.GetService<ISysListService>();
            List<SelGridColumn> columns = new List<SelGridColumn>();
            columns.Add(new SelGridColumn("SYS_ID", "系统ID", false));
            columns.Add(new SelGridColumn("SYS_NM", "系统名称"));
            var sysLists = sysListService.GetAllActiveSys();
            sysLists.Insert(0, new SysList() { SYS_ID = -1, SYS_NAME = "" });
            InitLookUpEdit(lookUp, sysLists, columns, "SYS_NM", "SYS_ID");
        }

        private static void InitLookUpEdit(LookUpEdit lookUpEdit, object datasource, List<SelGridColumn> columnsSet, string displayMember, string valueMember)
        {
            DataBinder.BindingLookupEditDataSource(lookUpEdit, datasource, displayMember, valueMember);
            lookUpEdit.Properties.Columns.Clear();
            foreach (var selColumn in columnsSet)
            {
                LookUpColumnInfo column = new LookUpColumnInfo();
                column.FieldName = selColumn.ColCode;
                column.Caption = selColumn.ColName;
                column.Visible = selColumn.ColShow;
                if (selColumn.ColWidth > 0)
                {
                    column.Width = selColumn.ColWidth;
                }

                lookUpEdit.Properties.Columns.Add(column);
            }
        }

        public static object Selector(SelectorData selector)
        {
            CommonSelector commonSelector = new CommonSelector(selector);
            if (selector.WindowHeight != null && selector.WindowWidth != null)
            {
                commonSelector.Size = new Size(selector.WindowWidth.Value, selector.WindowHeight.Value);
            }
            if (commonSelector.ShowDialog() == DialogResult.OK)
            {
                return commonSelector.GetSelectData();
            }
            return null;
        }
    }
}